package com.york.user.controller;

import com.york.bean.Result;
import com.york.entity.PageResult;
import com.york.user.entity.dto.UserDto;
import com.york.user.entity.po.UserPo;
import com.york.user.entity.req.UserListReq;
import com.york.user.entity.req.UserReq;
import com.york.user.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @PostMapping("/add")
    public Result<Boolean> addUser(@RequestBody UserReq userReq) {
        UserDto userDto = new UserDto();
        BeanUtils.copyProperties(userReq, userDto);
        return Result.ok(userService.addUser(userDto) > 0);
    }

    @DeleteMapping("/delete/{id}")
    public Result<Boolean> addUser(@PathVariable Integer id) {
        return Result.ok(userService.delete(id));
    }

    @GetMapping
    public Result<PageResult<UserPo>> getPage(@RequestBody UserListReq userListReq) {
        UserDto userDto = new UserDto();
        System.out.println(userListReq.toString());
        BeanUtils.copyProperties(userListReq, userDto);
        return Result.ok(userService.getUserPage(userDto));
    }
}
